<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>03 demo2</title>
</head>

<body>
    <button class="btn">按钮</button>

    <script>
        var btn = document.querySelector('.btn');
        btn.onclick = function (e) {
            e.preventDefault(); // 阻止默认事件
            alert("点击了按钮，阻止跳转");
        }
    </script>
</body>

</html>